Types of Light Dependent Resistors
Applications
/* Name : main.c * Purpose : Source code for LDR interface with ATMEGA16. * Author : Gemicates * Date : 2017-09-11 * Website : www.gemicates.org * Revision : None */ #ifndef F_CPU #define F_CPU 8000000UL // 8 MHz clock speed #endif #include<avr/io.h> #include<util/delay.h> void LDR_init(void); unsigned int LDR_read(); int main(void) { unsigned int value; DDRB=0xFF; LDR_init(); // Initialization of ADC while(1) { value=LDR_read(); PORTB=value; _delay_ms(500); } } void LDR_init(void) // Initialization of ADC { ADMUX=(1<<REFS0); // AVcc with external capacitor at AREF ADCSRA=(1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0); // Enable ADC and set Prescaler division factor as 128 } unsigned int LDR_read() { ADMUX |=0x00; // selecting channel ADCSRA|=(1<<ADSC); // start conversion while(ADCSRA==0x9f); // waiting for ADIF, conversion complete ADCSRA|=(1<<ADIF); // clearing of ADIF, it is done by writing 1 to it return (ADC); }